﻿using System;
using Newtonsoft.Json;
using StackExchange.Redis;
using TdxHq.Invoker;

namespace TdxHq.Common.Provider
{
    public class HqRedisProvider
    {
        private static string HqRedisKey = "hq:l1:snapshot";
        private static String HqPublishKey = "hq:l1:publish";
        public static void SaveHq(string key, String quoteContent)
        {
            RedisUtils.GetInstance().HqRedis.GetDatabase().HashSet(HqRedisKey, key, quoteContent);
        }
        
        public static QuoteRt GetHq(string key)
        {
            string quoteJson = null;
            if (RedisUtils.GetInstance().HqRedis.GetDatabase().HashExists(HqRedisKey, key))
                quoteJson = RedisUtils.GetInstance().HqRedis.GetDatabase().HashGet(HqRedisKey, key);

            if (null == quoteJson)
                return null;

            return JsonConvert.DeserializeObject<QuoteRt>(quoteJson);
        }

        public static void PushHq(String key,String quoteContent)
        {
            String channel = $"{HqPublishKey}:{key}";
            RedisUtils.GetInstance().HqRedis.GetDatabase().Publish(channel, JsonConvert.SerializeObject(quoteContent));
        }
    }
}